package com.example.campsystem.controller;

import com.example.campsystem.entity.CustomApi;
import com.example.campsystem.entity.Favorites;
import com.example.campsystem.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/favorites")
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;
    //根据useerid获取收藏列表
    @GetMapping("/list")
    public ResponseEntity<CustomApi> getFavoritesList(@RequestParam Long userid) {
        CustomApi customApi = new CustomApi();
        try {
            List<Favorites> favoritesList = favoritesService.getFavoritesList(userid);
            customApi.setData(favoritesList);
            customApi.setCode(200);
            customApi.setMsg("获取成功");
            return ResponseEntity.ok(customApi);
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("获取失败：" + e.getMessage());
            customApi.setData(null);
            return ResponseEntity.ok(customApi);
        }
    }
    //根据userid和ttargetid，type删除收藏
    @GetMapping("/delete")
    public ResponseEntity<CustomApi> deleteFavorites(@RequestParam Long userid, @RequestParam Long targetid, @RequestParam String type) {
        CustomApi customApi = new CustomApi();
        try {
            Favorites favorites = new Favorites();
            favorites.setUserId(userid);
            favorites.setTargetId(targetid);
            favorites.setType(type);
            boolean result = favoritesService.delete(favorites);
            if (result) {
                customApi.setCode(200);
                customApi.setMsg("删除成功");
                customApi.setData(null);
            }
        } catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("删除失败：" + e.getMessage());
            customApi.setData(null);
        }
        return ResponseEntity.ok(customApi);
    }
    //根据userid和ttargetid，type添加收藏
    @GetMapping("/add")
    public ResponseEntity<CustomApi> addFavorites(@RequestParam Long userid, @RequestParam Long targetid, @RequestParam String type) {
        CustomApi customApi = new CustomApi();
        try {
            Favorites favorites = new Favorites();
            favorites.setUserId(userid);
            favorites.setTargetId(targetid);
            favorites.setType(type);
            boolean result = favoritesService.add(favorites);
            if (result) {
                customApi.setCode(200);
                customApi.setMsg("添加成功");
                customApi.setData(null);
            }
        }
        catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("添加失败：" + e.getMessage());
            customApi.setData(null);
        }
        return ResponseEntity.ok(customApi);
    }
    //检查是否已收藏
    @GetMapping("/check")
    public ResponseEntity<CustomApi> checkFavorites(@RequestParam Long userid, @RequestParam Long targetid, @RequestParam String type) {
        CustomApi customApi = new CustomApi();
        try {
            Favorites favorites = new Favorites();
            favorites.setUserId(userid);
            favorites.setTargetId(targetid);
            favorites.setType(type);
            boolean result = favoritesService.check(favorites);
            if (result) {
                customApi.setCode(200);
                customApi.setMsg("已收藏");
                customApi.setData(null);
            }
            else {
                customApi.setCode(200);
                customApi.setMsg("未收藏");
            }
        }
        catch (Exception e) {
            customApi.setCode(500);
            customApi.setMsg("检查失败：" + e.getMessage());
            customApi.setData(null);
        }
        return ResponseEntity.ok(customApi);
    }
}
